<?php

class AddressModel extends Model{
	
	public $table='address';
	
	public function add_data(){
		
		if(Q('post.province')=='请选择省'){
			$this->error='请选择省份';
			return FALSE;
		}
		if(Q('post.city')=='请选择市'){
			$this->error='请选择市';
			return FALSE;
		}
		if(Q('post.county')=='请选择县（区）'){
			$this->error='请选择县（区）';
			return FALSE;
		}
		if(!Q('post.street')){
			$this->error='请填写街道';
			return FALSE;
		}
		if(!Q('post.ctc')){
			$this->error='请填写联系人';
			return FALSE;
		}
		if(!Q('post.tel')){
			$this->error='请填写电话';
			return FALSE;
		}
		$data=array(
			'address'=>Q('post.province') . 	Q('post.city') . Q('post.county') . Q('post.street'),
			'ctc'=>Q('post.ctc'),
			'tel'=>Q('post.tel'),
			'defau'=>Q('post.defau'),
			'uid'=>$_SESSION['uid'],
		);
		if(Q('post.defau')==1){
			$this->update_data(array('uid'=>$_SESSION['uid']),array('defau'=>0));
		}
		return $this->add($data);
	}
	
	public function update_data($where=null,$data=null){
		
		return $this->where($where)->update($data);
	}
	
	public function get_all_data($where=null,$order=null){
		
		return $this->where($where)->order($order)->all();
	}
	
	
	public function set_default(){
		
		$uid=$_SESSION['uid'];
		$addressid=Q('get.addressid');
		$this->where(array('uid'=>$uid))->update(array('defau'=>0));
		return $this->where(array('addressid'=>$addressid))->update(array('defau'=>1));
	}
	
	
	public function del_data($where=null){
		
		return $this->where($where)->delete();
	}
	
}
















